/**
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import components.Ball;

/**
 * The frame with panel and buttons.
 */
public class BounceFrame extends JFrame
{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -5779639965199717012L;

	private BallPanel panel;

	public static final int DEFAULT_WIDTH = 450;

	public static final int DEFAULT_HEIGHT = 350;

	public static final int STEPS = 1000;

	public static final int DELAY = 3;

	/**
	 * Constructs the frame with the panel for showing the bouncing ball and Start and Close buttons
	 */
	public BounceFrame()
	{
		this.setSize(BounceFrame.DEFAULT_WIDTH, BounceFrame.DEFAULT_HEIGHT);
		this.setTitle("BounceThread");

		this.panel = new BallPanel();
		this.add(this.panel, BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		this.addButton(buttonPanel, "Start", new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				BounceFrame.this.addBall();
			}
		});

		this.addButton(buttonPanel, "Close", new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				System.exit(0);
			}
		});
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	/**
	 * Adds a bouncing ball to the canvas and starts a thread to make it bounce
	 */
	public void addBall()
	{
		Ball b = new Ball();
		this.panel.add(b);
		Runnable r = new BallRunnable(b, this.panel);
		Thread t = new Thread(r);
		t.start();
	}

	/**
	 * Adds a button to a container.
	 * 
	 * @param c the container
	 * @param title the button title
	 * @param listener the action listener for the button
	 */
	public void addButton(Container c, String title, ActionListener listener)
	{
		JButton button = new JButton(title);
		c.add(button);
		button.addActionListener(listener);
	}
}
